package org.kojo.tdrm.servlet;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.kojo.tdrm.utils.App;
import org.kojo.tdrm.utils.HttpUtils;

public class FBLogout extends HttpServlet {
	private static Logger log = Logger.getLogger(FBLogout.class.getName());
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.processRequest(req, resp);
	}

	public void processRequest(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession sess = req.getSession();
		String userId = (String)sess.getAttribute(App.USERID);
		String token = (String)sess.getAttribute(App.FBTOKEN);
		if(userId!=null){
			log.fine("login["+userId+"] Logout user="+userId);
			sess.removeAttribute(App.USERID);
			String logoutUrl = "https://www.facebook.com/logout.php?next="+App.BASE_DOMAIN+"&access_token="+token;
			HttpUtils http = new HttpUtils();
			http.getUrlContent(logoutUrl);
		
			req.getRequestDispatcher("/").forward(req, resp);
		}
	}

}
